package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.UserDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.mapper.UserMapper;
import com.itheima.health.pojo.*;
import com.itheima.health.service.*;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Reference
    private RoleService roleService;
    @Override
    public UserVO findUserInfoByUsername(String username) {
        //   根据账号查询用户所有角色和权限关键字
        UserVO userVO  =    baseMapper.findUserByUsername(username);
        //  2 根据 uid  查询角色信息
        Set<RoleVO> roleVOSet = baseMapper.findrolesByUid(userVO.getId());
        //  3.  根据  roleId ==查询 权限信息
        for (RoleVO roleVO : roleVOSet) {
            Set<Permission> permissionSet = baseMapper.findPermssionsByRoleId(roleVO.getId());
            roleVO.setPermissionSet(permissionSet);
        }
        userVO.setRoles(roleVOSet);
        return userVO;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //  mp   配置插件：  完成分页 查询
        QueryWrapper queryWrapper = new QueryWrapper();//   封装查询条件
        queryWrapper.eq("is_delete",0);
        Page<User> page =null ;
        if(StringUtils.isEmpty(queryPageBean.getQueryString())){
            page = page(new Page<User>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);

        }else{
            //   select  * from  xxxx where  name like  xxx  limit ?,?
            queryWrapper.like("name",queryPageBean.getQueryString());
            page = page(new Page<User>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }


        PageResult result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    @Override
    public void saveUser(UserDTO userDTO) {
        Integer id = userDTO.getId();
        //  id   修改操作  值 不为null      insert  操作  为null
        if(!StringUtils.isEmpty(id)){
            //   修改操作   先删除  中间表数据
            baseMapper.deleteRoleIdsByUserId(id);
        }
        //    检查组 信息录入  +  中间表数据录入
        saveOrUpdate(userDTO);  //    checkGroupDTO 存在id   update 检查组表信息修改
        id  = userDTO.getId();
        // 2  中间表录入数据
        for (Integer roleId : userDTO.getRoleIds()) {
            baseMapper.addUsserIdAndRoleID(id,roleId);
        }

    }

    @Override
    public Map findroleInfoByUserId(int id) {
        Map map  = new HashMap();
        List<Role> list = roleService.list();
        map.put("roles",list);
        //  根据检查组id  去中间表查询 对应检查项ids
        int[]  roleIds = baseMapper.findroleInfoByUserId(id);
        map.put("roleIds",roleIds);
        return map;
    }
}



